package com.bp.hearthealth.pulse.lnapps.ads.model

import android.app.Activity
import android.content.Context
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.google.android.ump.ConsentDebugSettings
import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.FormError
import com.google.android.ump.UserMessagingPlatform

class GoogleMobileAdsConsentManager private constructor(context: Context) {
    private val consentInformation: ConsentInformation =
        UserMessagingPlatform.getConsentInformation(context)

    /** 同意收集完成时调用回调的接口定义。 */
    fun interface OnConsentGatheringCompleteListener {
        fun consentGatheringComplete(error: FormError?)
    }

    /** 用于确定应用程序是否可以请求广告的辅助变量。 */
    val canRequestAds: Boolean
        get() = consentInformation.canRequestAds()

    /** 用于确定是否需要隐私选项表单的辅助变量。 */
    val isPrivacyOptionsRequired: Boolean
        get() = consentInformation.privacyOptionsRequirementStatus == ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED


    /**
     * 用于调用 UMP SDK 方法来请求同意信息并加载/显示
     * 如有必要，请填写同意书。
     */
    fun gatherConsent(
        activity: Activity, onConsentGatheringCompleteListener: OnConsentGatheringCompleteListener
    ) {
        // 出于测试目的，您可以强制将 DebugGeography 设置为 EEA 或 NOT_EEA。
        val debugSettings = ConsentDebugSettings.Builder(activity)
            // Check your logcat output for the hashed device ID e.g.
            // "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345")" to use
            // the debug functionality.
            .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
            .addTestDeviceHashedId("BA5F175035216FD5293788F3B2CA5AE3")
            .build()

        val params =
            ConsentRequestParameters.Builder()
//                .setConsentDebugSettings(debugSettings)
                .build()

        //每次应用程序启动时都应调用请求更新同意信息。
        consentInformation.requestConsentInfoUpdate(activity, params, {
            UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError ->
                LogUtil.i("已征得同意 $formError")
                // 已征得同意。
                onConsentGatheringCompleteListener.consentGatheringComplete(formError)
            }
        }, { requestConsentError ->
            LogUtil.i("requestConsentError: ${requestConsentError.message}")
            onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError)
        })
    }

    /** 调用 UMP SDK 方法以显示隐私选项表单的帮助程序方法。 */
    fun showPrivacyOptionsForm(
        activity: Activity, onConsentFormDismissedListener: OnConsentFormDismissedListener
    ) {
        UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener)
    }

    companion object {
        @Volatile
        private var instance: GoogleMobileAdsConsentManager? = null

        fun getInstance(context: Context) = instance ?: synchronized(this) {
            instance ?: GoogleMobileAdsConsentManager(context).also { instance = it }
        }
    }
}
